const Permission = require('../mongooses/rbac/Permission')
const MenuTree = require('../../utils/tree')
class MenuController {
    async get (ctx, next) {
        let system_key = ctx.request.body.system_key
        let permissions = await Permission.find({system_key: system_key}).lean();
        let menus_data = [] 

        permissions.forEach(permission => {
            let item =  {}
            item['_id'] = permission._id,
            item['parent_id'] = permission.parent_id,
            item['name'] = permission.permission_name,
            item['routename'] = permission.route_name,
            item['icon'] = permission.icon,
            item['sort'] = permission.sort,
            menus_data.push(item)
        }, this)

        let menus = MenuTree.getMenus(menus_data)
        ctx.body = menus
    }

}

module.exports = new MenuController()